﻿namespace FlyingHat.Model.Models.Global
{
    public interface IReturnModel
    {
        /// <summary>
        /// 返回数据状态码：0=正常，1=错误
        /// </summary>
        int Code { get; set; }
        /// <summary>
        /// 消息
        /// </summary>
        string Msg { get; set; }
    }
    public interface IReturnModel<out T> : IReturnModel
    {
        /// <summary>
        /// 数据
        /// </summary>
        T? Data { get; }
    }
    public class ReturnModel<T> : IReturnModel<T>
    {
        /// <summary>
        /// 返回数据状态码：0=正常，1=错误
        /// </summary>
        public int Code { get; set; }

        /// <summary>
        /// 消息
        /// </summary>
        public string Msg { get; set; } = string.Empty;

        /// <summary>
        /// 数据
        /// </summary>
        public T? Data { get; set; }
        public ReturnModel<T> Success(T data, string message)
        {
            Code = 0;
            Msg = message;
            Data = data;
            return this;
        }
        public ReturnModel<T> Fail(int code, T data, string message)
        {
            Code = code;
            Msg = message;
            Data = data;
            return this;
        }

    }

    public static class ReturnModel
    {
        public static IReturnModel Page()
        {
            return new ReturnPageModel<int>(0, "获取成功", new List<int>(), 0);
        }
        public static IReturnModel Page<T>(List<T> list)
        {
            return new ReturnPageModel<T>(0, "获取成功", list, list.Count);
        }
        public static IReturnModel Page<T>(long count, List<T> list)
        {
            return new ReturnPageModel<T>(0, "获取成功", list, count);
        }
        public static IReturnModel Success(string message = "操作成功")
        {
            return Success<string>(message, "");
        }
        public static IReturnModel Success<T>(string message, T data)
        {
            return new ReturnModel<T>().Success(data, message);
        }
        public static IReturnModel Fail(string message = "操作失败")
        {
            return Fail<string>(message, "");
        }
        public static IReturnModel Fail<T>(string message, T data)
        {
            return Fail(1, message, data);
        }
        public static IReturnModel Fail(int code, string message)
        {
            return Fail<string>(code, message, "");
        }
        public static IReturnModel Fail<T>(int code, string message, T data)
        {
            return new ReturnModel<T>().Fail(code, data, message);
        }
        public static IReturnModel Result<T>(bool success, T data, string message, bool prefix = true)
        {
            if (string.IsNullOrWhiteSpace(message))
            {
                return success ? Success("操作成功", data) : Fail("操作失败", data);
            }
            if (prefix)
            {
                return prefix ? Success($"{message}成功！", data) : Fail($"{message}失败！", data);
            }
            return success ? Success(message, data) : Fail(message, data);
        }
        public static IReturnModel Result(bool success, string message = "", bool prefix = true)
        {
            if (string.IsNullOrWhiteSpace(message))
            {
                return success ? Success() : Fail();
            }
            if (prefix)
            {
                return prefix ? Success($"{message}成功！") : Fail($"{message}失败！");
            }
            return success ? Success(message) : Fail(message);
        }
    }
    public class ReturnPageModel<T> : IReturnModel
    {
        public ReturnPageModel(int code, string msg, List<T> list, long count)
        {
            this.Code = code;
            this.Msg = msg;
            this.Data = list;
            this.Count = count;
        }
        /// <summary>
        /// 返回数据状态码：0=正常，1=错误
        /// </summary>
        public int Code { get; set; } = 0;

        /// <summary>
        /// 消息
        /// </summary>
        public string Msg { get; set; } = string.Empty;

        /// <summary>
        /// 数据
        /// </summary>
        public List<T> Data { get; set; } = new List<T> { };
        public long Count { get; set; } = 0;

    }

}
